﻿using System;
using System.Collections.Generic;
using System.Text;
using SQLRunnerLib.Services;

namespace SQLRunnerLib
{

    public enum AuthenticationType
    {
        WindowsAuthentication,
        SQLServerAuthentication
    }

    [Serializable()]
    public class Environment
    {
        private string _name;
        private string _serverName;
        private string _databaseName;
        private AuthenticationType _authentication = AuthenticationType.WindowsAuthentication;
        private string _username;
        private string _password;
        private bool _savePassword = false;
        private bool _isSelected;
        private List<PlaceHolder> _placeHolders = new List<PlaceHolder>();

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string ServerName
        {
            get { return _serverName; }
            set { _serverName = value; }
        }

        public string DatabaseName
        {
            get { return _databaseName; }
            set { _databaseName = value; }
        }

        public AuthenticationType Authentication
        {
            get { return _authentication; }
            set { _authentication = value; }
        }

        public bool Trusted
        {
            get { return (_authentication == AuthenticationType.WindowsAuthentication); }
        }

        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public bool SavePassword
        {
            get { return _savePassword; }
            set { _savePassword = value; }
        }

        public bool IsSelected
        {
            get { return _isSelected; }
            set { _isSelected = value; }
        }

        public List<PlaceHolder> PlaceHolders
        {
            get { return _placeHolders; }
            set { _placeHolders = value; }
        }

        /// <summary>
        /// Checks whether
        /// </summary>
        public bool HasFullCredentials
        {
            get
            {
                bool result = false;
                if (Authentication == AuthenticationType.WindowsAuthentication)
                {
                    result = !string.IsNullOrEmpty(ServerName) && !string.IsNullOrEmpty(DatabaseName);
                }
                else
                {
                    result = !string.IsNullOrEmpty(ServerName) && !string.IsNullOrEmpty(DatabaseName) &&
                    !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password);
                }
                return result;
            }
        }

        public void OpenConnection()
        {
            if (HasFullCredentials)
            {
                ServiceContainer.SQLConnection.OpenConnection(_serverName, _username, _password, (_authentication == AuthenticationType.WindowsAuthentication));
                ServiceContainer.SQLConnection.GetDatabase(_databaseName);
            }
            else
            {
                // TODO: Inform user of missing credentials
            }
        }

    }
}
